
/*
 * @Descripttion: 
 * @version: 
 * @Author: 高月飞
 * @Date: 2023-11-17 21:34:02
 * @LastEditors: 高月飞
 * @LastEditTime: 2023-11-24 01:59:12
 */

//网络请求
import { UserLoginPasswordApi } from '@/services/login'
import { defineStore } from "pinia";


const useUserStore = defineStore('user', {
    //持久化操作
    //对于登录信息进行一次持久化操作
    persist: {
        storage: window.sessionStorage,
        paths: ['userinfo']
    },
    state: () => ({
        userinfo: {
            userid: 0,
            name: "",
            phone: "",
            token: "",
            roleid: 0
        }
    }) as IUserModule.IStateStore,
    /**
     * actions方法
     * 得到返回值
     * 用户退出
     * 异步进行请求数据----要给返回值 需要在登陆成功后 进行路由跳转
     */
    actions: {
        SETUSERINFO(user: IUserModule.IApiUserData) {
            //把返回的数据给userinfo
            this.userinfo = user
        },
        SETUSERLOGOUT() {
            this.userinfo = {
                userid: 0,
                name: "",
                phone: "",
                token: "",
                roleid: 0
            }
        },
        async USERLOGINACTION(loginDate: IUserModule.ILoginPasswordFrom) {
            let ret = await UserLoginPasswordApi(loginDate)
            // console.log(ret)
            if (ret.code === 200) {
                this.SETUSERINFO(ret.data)
            }
            return ret.code
        }
    },
})
export default useUserStore
